<?php
class DynamicModuleManageAction extends BaseAction {
	
	const TYPE = 'dy_module';
	
	public function index() {
		
		$list = DynamicModuleHelper::getList();
		
		$this->assign("list", $list);
		$this->assign("type", self::TYPE);
		$this->display();
	}
	
	//[操作流]默认页面{修改} Or {新增}
	//当传过来的ID有值，则进行修改操作流
	//当传过来的ID没值，则进行新增操作流
	public function todo(){
		$referUrl = $_SERVER['HTTP_REFERER'];
		$id = $this->_get("id");
		$obj = DynamicModuleHelper::getObject("dynamic_module_id={$id}");
		
		$this->assign("obj",$obj);
		$this->assign("referUrl",$referUrl);
		$this->assign("type", self::TYPE);
		$this->display();
	}
	
	public function add(){
		
		$data['module_name'] = $this->_post("module_name");
		$data['table_name'] = $this->_post("table_name");
		$data['module_desc'] = $this->_post('module_desc');
		
		//表单名称验证
		if(empty($data['module_name'])){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "模型名称不能为空！");
			return;
		}
		//检查table_name是否存在
		$sql = "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_NAME='".C("DB_PREFIX").$data['table_name']."' AND TABLE_SCHEMA='".C("DB_NAME")."'";
		$count = ArticleHelper::queryForInt($sql);
		if($count > 0){
			echo CommonUtil::ajaxReturn(Status::$ERROR, $data['table_name']."表名已存在，请重新输入！");
			return;
		}
		
		//.....增加一个DynamicModule以及相应Field记录
		$result = DynamicModuleHelper::addWithTrans($data);
		if($result === false){	//失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return ;
		}
		
		//.....创建相应数据库
		$sql = "CREATE TABLE `".C("DB_PREFIX").$data['table_name']."` (
				   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
				   `seo_keywords` varchar(100) DEFAULT NULL COMMENT 'SEO关键词',
				   `seo_description` varchar(100) DEFAULT NULL COMMENT 'SEO描述',
				   `catalog_id` int(10) unsigned NOT NULL COMMENT '栏目ID',
				   `view_num` int(11) NOT NULL DEFAULT '0' COMMENT '浏览量',
				   `redirect_url` varchar(150) DEFAULT NULL COMMENT '跳转URL',
				   `timeline` int(11) unsigned NOT NULL DEFAULT '1396407611' COMMENT '发布时间',
				   PRIMARY KEY (`id`)
				 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
		
		DynamicModuleHelper::updateSQL($sql);
		
		echo CommonUtil::ajaxReturn(Status::$REDIRECT,null, __APP__.'/center/DynamicModuleManage/');
		
	}
	
	public function update(){
		$id = $this->_post("id");
		$data['module_name'] = $this->_post("module_name");
		$data['table_name'] = $this->_post("table_name");
		$data['module_desc'] = $this->_post('module_desc');
		
		$obj = DynamicModuleHelper::getObject("dynamic_module_id={$id}");
		if(empty($obj)){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return;
		}
		//表单名称验证
		if(empty($data['module_name'])){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "表单名称不能为空！");
			return;
		}
		if($obj["table_name"] != $data['table_name']){
			
			//检查$data['table_name']是否存在
			$sql = "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_NAME='".C("DB_PREFIX").$data['table_name']."' AND TABLE_SCHEMA='".C("DB_NAME")."'";
			$count = ArticleHelper::queryForInt($sql);
			if($count > 0){	//存在，提示报错信息
				echo CommonUtil::ajaxReturn(Status::$ERROR, $data['table_name']."表名已存在，请重新输入！");
				return;
			}
			
		}
		
		$result = DynamicModuleHelper::update($data,"dynamic_module_id={$id}");
		if($result === false){	//失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return ;
		}
		
		if($obj["table_name"] != $data['table_name']){	//如果不想等，则需要进行alter table
			
			//检查$obj['table_name']是否存在
			$sql = "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_NAME='".C("DB_PREFIX").$obj['table_name']."' AND TABLE_SCHEMA='".C("DB_NAME")."'";
			$count = ArticleHelper::queryForInt($sql);
			if($count > 0){	//存在，进行alter table
				//...ALTER TABLE操作
				$sql = "ALTER TABLE ".C("DB_PREFIX").$obj['table_name']." RENAME TO ".C("DB_PREFIX").$data['table_name'];
				ReserveModuleHelper::updateSQL($sql);
			}
			
		}
		
		echo CommonUtil::ajaxReturn(Status::$REDIRECT,"修改成功！", __APP__.'/center/DynamicModuleManage/');
		
	}
	
	public function delete() {
		
		$id = $this->_get("id");
		
		//该ID是否存在
		$obj = DynamicModuleHelper::getObject("dynamic_module_id={$id}");
		if(empty($obj)){
			echo CommonUtil::ajaxReturn(Status::$SUCCESS, "对象不存在，操作失败！");
			return ;
		}
		if($obj["is_system"] == 1){
			echo CommonUtil::ajaxReturn(Status::$SUCCESS, "系统表不能删除！");
			return ;
		}
		//是否有 栏目 在使用该模块...
		$list = CatalogHelper::getList("catalog_type='".CatalogType::$RESERVE_MODULE."' AND dynamic_module_id=".$id);
		if(!empty($list)){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "该模型已经在使用中，请删除栏目后再进行删除！");
			return ;
		}
		//删除是否成功
		$result = DynamicModuleHelper::deleteWithTrans($id);
		if($result === false){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "系统繁忙，请稍后重试！");
			return ;
		}
		
		//DROP TABLE
		$sql = "DROP TABLE IF EXISTS `".C("DB_PREFIX").$obj["table_name"]."`;";
		DynamicModuleHelper::updateSQL($sql);
		
		echo CommonUtil::ajaxReturn(Status::$RELOAD_PAGE, "操作成功！");
		
	}
	
	
	
}